from mcp.server.fastmcp import FastMCP
from mcp.server.auth.settings import AuthSettings
from pydantic import AnyHttpUrl
from typing import Any
from datetime import datetime
from verifier import IntrospectionTokenVerifier

server_host = "localhost"
server_port = 8080

issuer_host = "localhost"
issuer_port = 9090

app = FastMCP(
    name="OAuth2 Demo",
    instructions="Resource Server that validates tokens via Authorization Server introspection",
    host=server_host,
    port=server_port,
    debug=True,
    token_verifier=IntrospectionTokenVerifier(
        introspect_endpoint=f"http://{issuer_host}:{issuer_port}/introspect",
    ),
    auth=AuthSettings(
        # 授权服务器的URL
        issuer_url=AnyHttpUrl(f"http://{issuer_host}:{issuer_port}/"),
        required_scopes=['user'],
        resource_server_url=AnyHttpUrl(f"http://{server_host}:{server_port}")
    )
)

@app.tool()
async def get_time() -> dict[str, Any]:
    """
    Get the current server time
    """
    now = datetime.now()

    return {
        "current_time": now.isoformat(),
        "timezone": "UTC",
        "timestamp": now.timestamp(),
        "formatted":now.strftime("%Y-%m-%d %H:%M:%S"),
    }


if __name__ == '__main__':
    app.run(transport="streamable-http")